<?php

namespace lh\controller;

use \GatewayWorker\Lib\Gateway;

class Hall{
  //进入大厅
  static function enter($cid,$name){
    //返回大厅所有人、房间列表、你的ID
    $data=['user'=>(Object)\LH::$hallUser,'room'=>(Object)\LH::$roomList,'id'=>$cid];
    $result=json_encode(['r'=>'/hall/list','data'=>$data]);
    Gateway::sendToClient($cid, $result);
    //加入大厅组
    Gateway::joinGroup($cid, 'hall');
    \LH::$hallUser[$cid]=['name'=>$name];
    //通知大厅(包括进入者)
    $data=['id'=>$cid,'name'=>$name];
    $result=json_encode(['r'=>'/hall/enter','data'=>$data]);
    Gateway::sendToGroup('hall',$result);
  }

  //聊天
  static function chat($cid,$content){
    $data=['name'=>\LH::$hallUser[$cid]['name'],'content'=>$content];
    $result=json_encode(['r'=>'/hall/chat','data'=>$data]);
    Gateway::sendToGroup('hall',$result);
  }

  //建房
  static function setRoom($cid,$name){
    //创建房间
    \LH::$hallUser[$cid]['room']=$cid;
    $user=[$cid=>['hero'=>'','ready'=>false]];
    $room=['name'=>$name,'now'=>1,'num'=>4,'user'=>$user];
    \LH::$roomList[$cid]=$room;
    Gateway::joinGroup($cid, $cid);
    //通知大厅有人建房
    $data=['id'=>$cid,'name'=>$name,'now'=>1,'num'=>4];
    $result=json_encode(['r'=>'/hall/room/set','data'=>$data]);
    Gateway::sendToGroup('hall',$result);
  }
}